package avicit.bdp.dgs.qa.dto;

import avicit.bdp.common.base.BaseDTO;
import avicit.bdp.dgs.qa.utils.enums.CompareType;
import avicit.platform6.core.annotation.log.FieldRemark;
import avicit.platform6.core.annotation.log.Id;
import avicit.platform6.core.annotation.log.LogField;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import javax.persistence.Column;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2023-03-30
 * @类说明：AlarmConfigDTO
 * @修改记录：
 * @注意事项：
 * @主要功能：规则校验DTO
 */
@ApiModel(value = "AlarmConfigDTO", description = "规则校验DTO")
@Table(name = "bdp_dgs_qa_rule_alarm_config")
public class AlarmConfigDTO extends BaseDTO {
    /**
     * 主键
     */
    @Id
    @LogField
    @ApiModelProperty(value = "主键")
    @FieldRemark(column = "id", field = "id", name = "id")
    @javax.persistence.Id
    @Column(name = "id")
    private String id;

    /**
     * 规则ID
     */
    @LogField
    @ApiModelProperty(value = "规则ID")
    @FieldRemark(column = "ruleId", field = "ruleId", name = "ruleId")
    @Column(name = "rule_id")
    private String ruleId;

    /**
     * 模板ID
     */
    @LogField
    @ApiModelProperty(value = "模板ID")
    @FieldRemark(column = "templateId", field = "templateId", name = "templateId")
    @Column(name = "template_id")
    private String templateId;

    /**
     * 校验类型
     */
    @LogField
    @ApiModelProperty(value = "校验类型，0:数值型、1:波动率型")
    @FieldRemark(column = "checkType", field = "checkType", name = "checkType")
    @Column(name = "check_type")
    private Integer checkType;
    @Transient
    private String checkTypeName;

    /**
     * 校验方式
     */
    @LogField
    @ApiModelProperty(value = "校验方式，0:与固定值比较、1:7天平均值波动、2:30天平均值波动、3:1天周期比较、4:7天周期比较、5:30天周期比较、6:7天方差比较、7:30天方差比较、8:1.7.30天波动检车、9:上一周期比较")
    @FieldRemark(column = "checkMode", field = "checkMode", name = "checkMode")
    @Column(name = "check_mode")
    private Integer checkMode;
    @Transient
    private String checkModeName;

    /**
     * 模板输出名称
     */
    @LogField
    @ApiModelProperty(value = "模板输出名称")
    @FieldRemark(column = "outputName", field = "outputName", name = "outputName")
    @Column(name = "output_name")
    private String outputName;

    /**
     * 期望值：总行数
     * >10 - <100
     */
    /**
     * 比较类型
     */
    @LogField
    @ApiModelProperty(value = "比较类型")
    @FieldRemark(column = "lowerCompareType", field = "lowerCompareType", name = "lowerCompareType")
    @Column(name = "lower_compare_type")
    private Integer lowerCompareType;
    @Transient
    private String lowerCcompareTypeName;

    /**
     * 阈值
     */
    @LogField
    @ApiModelProperty(value = "阈值")
    @FieldRemark(column = "lowerThreshold", field = "lowerThreshold", name = "lowerThreshold")
    @Column(name = "lower_threshold")
    private Double lowerThreshold;

    /**
     * 比较类型
     */
    @LogField
    @ApiModelProperty(value = "比较类型")
    @FieldRemark(column = "upperCompareType", field = "upperCompareType", name = "upperCompareType")
    @Column(name = "upper_compare_type")
    private Integer upperCompareType;
    @Transient
    private String upperCcompareTypeName;

    /**
     * 阈值
     */
    @LogField
    @ApiModelProperty(value = "阈值")
    @FieldRemark(column = "upperThreshold", field = "upperThreshold", name = "upperThreshold")
    @Column(name = "upper_threshold")
    private Double upperThreshold;

    /**
     * 脏数据处理方式，0：不保存脏数据、1：保存脏数据
     */
    @LogField
    @ApiModelProperty(value = "阈值")
    @FieldRemark(column = "dirtyDataType", field = "dirtyDataType", name = "dirtyDataType")
    @Column(name = "dirty_data_type")
    private Integer dirtyDataType;

    /**
     * 告警校验公式，将字段、阈值、比较类型组合起来
     */
    @Transient
    private String formula;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getRuleId() {
        return ruleId;
    }

    public void setRuleId(String ruleId) {
        this.ruleId = ruleId;
    }

    public String getTemplateId() {
        return templateId;
    }

    public void setTemplateId(String templateId) {
        this.templateId = templateId;
    }

    public Integer getCheckType() {
        return checkType;
    }

    public void setCheckType(Integer checkType) {
        this.checkType = checkType;
    }

    public String getCheckTypeName() {
        return checkTypeName;
    }

    public void setCheckTypeName(String checkTypeName) {
        this.checkTypeName = checkTypeName;
    }

    public Integer getCheckMode() {
        return checkMode;
    }

    public void setCheckMode(Integer checkMode) {
        this.checkMode = checkMode;
    }

    public String getCheckModeName() {
        return checkModeName;
    }

    public void setCheckModeName(String checkModeName) {
        this.checkModeName = checkModeName;
    }

    public String getOutputName() {
        return outputName;
    }

    public void setOutputName(String outputName) {
        this.outputName = outputName;
    }

    public Integer getLowerCompareType() {
        return lowerCompareType;
    }

    public void setLowerCompareType(Integer lowerCompareType) {
        this.lowerCompareType = lowerCompareType;
    }

    public String getLowerCcompareTypeName() {
        return lowerCcompareTypeName;
    }

    public void setLowerCcompareTypeName(String lowerCcompareTypeName) {
        this.lowerCcompareTypeName = lowerCcompareTypeName;
    }

    public Double getLowerThreshold() {
        return lowerThreshold;
    }

    public void setLowerThreshold(Double lowerThreshold) {
        this.lowerThreshold = lowerThreshold;
    }

    public Integer getUpperCompareType() {
        return upperCompareType;
    }

    public void setUpperCompareType(Integer upperCompareType) {
        this.upperCompareType = upperCompareType;
    }

    public String getUpperCcompareTypeName() {
        return upperCcompareTypeName;
    }

    public void setUpperCcompareTypeName(String upperCcompareTypeName) {
        this.upperCcompareTypeName = upperCcompareTypeName;
    }

    public Double getUpperThreshold() {
        return upperThreshold;
    }

    public void setUpperThreshold(Double upperThreshold) {
        this.upperThreshold = upperThreshold;
    }

    public Integer getDirtyDataType() {
        return dirtyDataType;
    }

    public void setDirtyDataType(Integer dirtyDataType) {
        this.dirtyDataType = dirtyDataType;
    }

    /**
     * 获取校验公式，标准形式为"（字段名 + 操作符 + 左边阈值） && （字段名 + 操作符 + 右边阈值）"
     *  1、未配置告警，无校验公式；
     *  2、仅配置左边操作符、左边阈值，校验公式为“字段名 + 操作符 + 左边阈值”
     *  3、仅配置右边操作符、右边阈值，校验公式为“字段名 + 操作符 + 右边阈值”
     *  4、左右操作符、阈值均配置，校验公式为“（字段名 + 操作符 + 左边阈值） && （字段名 + 操作符 + 右边阈值）”
     * @return
     */
    public String getFormula() {
        StringBuffer sb = new StringBuffer();

        boolean hasConfigExpr = false;// 已经设置了校验公式
        boolean leftFormulaExist = false;// 左侧表达式已配置
        boolean rightFormulaExist = false;// 右侧表达式已配置
        if (lowerCompareType != null && lowerThreshold != null) {
            leftFormulaExist = true;
        }
        if (upperCompareType != null && upperThreshold != null) {
            rightFormulaExist = true;
        }

        if (leftFormulaExist && rightFormulaExist) {
            sb.append("value ").append(CompareType.getDescByCode(lowerCompareType)).append(" ").append(lowerThreshold)
                    .append( " && ")
                    .append("value ").append(CompareType.getDescByCode(upperCompareType)).append(" ").append(upperThreshold);
            hasConfigExpr = true;
        }
        if (!hasConfigExpr && leftFormulaExist) {
            sb.append("value ").append(CompareType.getDescByCode(lowerCompareType)).append(" ").append(lowerThreshold);
            hasConfigExpr = true;
        }
        if (!hasConfigExpr && rightFormulaExist) {
            sb.append("value ").append(CompareType.getDescByCode(upperCompareType)).append(" ").append(upperThreshold);
        }

        formula = sb.toString();
        return formula;
    }

    public void setFormula(String formula) {
        this.formula = formula;
    }
}
